<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <div class="flex mt5">
      <el-card shadow="never" :body-style="{ padding: '5px 10px 0px' }" style="margin-right: 10px">
        <div slot="header" class="f16">
          <span style="font-weight: 600">{{ $t(`Print.PickingListInquiry`) }}</span>
        </div>
        <Table :isLoading="isLoading" :tableHeader="tableHeader" :tableData="tableData" class="mt5"
          @currentRow="currentRow" />
      </el-card>
      <el-card shadow="never" :body-style="{ padding: '5px 10px 0px' }">
        <div slot="header" class="f16">
          <span style="font-weight: 600">{{ $t(`Print.DetailedInquiryOfActualInventions`) }}</span>
        </div>
        <Table :tableHeader="tableHeader2" :tableData="tableData2" class="mt5" />
      </el-card>
    </div>

    <el-card shadow="never" :body-style="{ padding: '4px 10px' }">
      <PageModule :PageData="{ total }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
    </el-card>
  </div>
</template>
<script lang="ts">
import { defineComponent, onMounted, ref, watch } from 'vue'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'MaterialOutboundQuery'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import Find from '@/components/Find/index.vue'
import { selectList } from '@/plugins/mixin'
import fileRequest from '@/utils/fileRequest'
import Synchronization from '@/utils/Synchronization'
import { ElMessage } from 'element-plus'
import { debounce } from 'lodash'
const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入指令',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入领料单号',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '领料类型',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '请输入订购单号',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入材料名称',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '补料部门',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '分类',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'date',
    dateLabel: '领料日期',
    dateValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '导出'
  }
]) as any
// 表格数据
const tableData = ref([])
const tableHeader = ref([
  {
    label: '领料类型',
    prop: 'slipType'
  },
  {
    label: '指令',
    prop: 'lotNo'
  },
  {
    label: '部门名称',
    prop: 'deptName'
  },
  {
    label: '领料单号',
    prop: 'slipNo',
    width: 90
  },
  {
    label: '领料日期',
    prop: 'slipDate'
  },
  {
    label: '物料编码',
    prop: 'mtlNo',
    width: 120
  },
  {
    label: '材料颜色',
    prop: 'mtlColor',
    width: 120
  },
  {
    label: '材料名称',
    prop: 'mtlName',
    width: 280
  },
  {
    label: '规格',
    prop: 'mtlSpec'
  },
  {
    label: '单位',
    prop: 'mtlUnit'
  },
  {
    label: '请领量',
    prop: 'reqQty'
  },
  {
    label: '已实发量',
    prop: 'actQty'
  },
  {
    width: '120px',
    label: '补料单号',
    prop: 'supplementNo'
  },
  {
    label: '备注',
    prop: 'remark'
  }
])
// 表一模块
// '置空'按钮
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
    getTableList()
  }
  if (content === '导出') {
    const res = (await fileRequest.post(`/material/exportMaterialDetailOut`, {
      deptName: formList.value
        .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '补料部门')
        ?.selectValue?.split(' ')[1],
      deptNo: formList.value
        .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '补料部门')
        ?.selectValue?.split(' ')[0],
      lotNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入指令'
      )?.inputValue,
      mtlName: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入材料名称'
      )?.inputValue,
      mtlNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入物料编码'
      )?.inputValue,
      slipDateEnd: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '领料日期'
      )?.dateValue?.[1],
      slipDateStart: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '领料日期'
      )?.dateValue?.[0],
      slipNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入领料单号'
      )?.inputValue,
      slipType: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '领料类型')
        ?.selectValue,
      typeNo: formList.value
        .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '分类')
        ?.selectValue?.split(' ')[0],
      pageNum: currentPage.value,
      pageSize: pageSize.value
    })) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`filename=`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
}
const isLoading = ref(true)

// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}

// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await Synchronization.post<any, any>('/material/getMaterialDetailOut', {
    deptName: formList.value
      .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '补料部门')
      ?.selectValue?.split(' ')[1],
    deptNo: formList.value
      .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '补料部门')
      ?.selectValue?.split(' ')[0],
    lotNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入指令'
    )?.inputValue,
    mtlName: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入材料名称'
    )?.inputValue,
    mtlNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入物料编码'
    )?.inputValue,
    slipDateEnd: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '领料日期'
    )?.dateValue?.[1],
    slipDateStart: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '领料日期'
    )?.dateValue?.[0],
    slipNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入领料单号'
    )?.inputValue,
    slipType: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '领料类型')
      ?.selectValue,
    typeNo: formList.value
      .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '分类')
      ?.selectValue?.split(' ')[0],
    pageNum: currentPage.value,
    pageSize: pageSize.value
  })
  isLoading.value = false
  tableData.value = tableDataList.materialDetailOutList
  total.value = tableDataList.total
  currentRow()
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
const getSelectList = async () => {
  const type = formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '分类') as any
  const mtlType = formList.value.find(
    (item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '领料类型'
  ) as any
  const mtlDept = formList.value.find(
    (item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '补料部门'
  ) as any
  const res = await Synchronization.get<any, any>('/back/getMtlType3')
  const mtlDeps = await Synchronization.get<any, any>('/back/deptQuery')
  res.forEach((Vendor: any) => {
    type.selectOptions.push({
      optionLabel: Vendor.typeName,
      optionValue: `${Vendor.typeNo}  ${Vendor.typeName}`,
      LeftLabel: `${Vendor.typeNo}`,
      RightLabel: `${Vendor.typeName}`
    })
  })
  mtlDeps.forEach((Vendor: any) => {
    mtlDept.selectOptions.push({
      optionLabel: Vendor.deptName,
      optionValue: `${Vendor.deptNo}  ${Vendor.deptName}`,
      LeftLabel: `${Vendor.deptNo}`,
      RightLabel: `${Vendor.deptName}`
    })
  })
  mtlType.selectOptions.push(
    { optionValue: '指令领料', optionLabel: '指令领料' },
    { optionValue: '补料领料', optionLabel: '补料领料' },
    { optionValue: '非指令领料', optionLabel: '非指令领料' }
  )
}
onMounted(() => {
  getSelectList()
})
// 底部表格数据
const tableData2 = ref([])

const tableHeader2 = ref([
  {
    label: '实发日期',
    prop: 'slipDate',
    width: 100
  },
  {
    label: '实发量',
    prop: 'actQty',
    width: 100
  }
])
// 点击表给另一张表传数据
const currentRow = async (row: any = tableData.value[0] ? tableData.value[0] : {}) => {
  let { lotNo, mtlNo, slipNo }: any = row
  // 给表二传指令号
  let table2Data = await Synchronization.post<any, any>(`/material/getSendOutList`, {
    lotDetail: lotNo,
    mtlNo,
    purNo: slipNo
  })
  tableData2.value = table2Data.sendList
}
</script>

<style lang="less" scoped>
:deep(.el-form-item:nth-last-child(5)) {
  .el-select {
    width: 150px;
    height: 28px;
  }
}
</style>
